<?php
/**
 * Interface for an instructor to add new textbooks, and select required textbooks from a list
 */
?>
<html>
<head>

</head>
<body>
<?php
require_once '../dbms_university_courses_mvc/Connection.php';
require_once '../dbms_university_courses_mvc/Model/model_require_once.php';

/* @var $instructor Instructor */
$instructors = Instructor::getInstructors_by_profile_id ( $_SESSION ['profile_id'] );
$instructor = $instructors [0];

$instructorDepartments = InstructorDepartment::getInstructorDepartments_by_instructor_id ( $instructor->get_instructor_id () );
?>
<?php

//get all the courses that currently have no instructor
$courses = Course::getCourses ();
?>
<script type="text/javascript">
	<?php
	/* @var $classroom Classroom */
	foreach ( $courses as $course ) {
		?>
	var Course<?php
		echo $course->get_course_id ();
		?> = new Course(<?php
		echo json_encode ( $course );
		?>);
		
	function Course<?php
		echo $course->get_course_id ();
		?>SelectClassroom(id){
		Course<?php
		echo $course->get_course_id ();
		?>.set_classroom_id(id, refreshPage);
	}
	<?php
	}
	?>
</script>
<h1>Your Courses</h1>
<table>
	<tr>
		<th>Course</th>
		<th>Textbook(s)</th>
	</tr>
<?php
foreach ( $courses as $course ) {
	if ($course->get_instructor_id () == $instructor->get_instructor_id ()) {
		?>
		<?php
		$courseTextbooks = CourseTextbook::getCourseTextbooks_by_course_id ( $course->get_course_id () );
		/* @var $courseTextbook CourseTextbook */
		foreach ( $courseTextbooks as $courseTextbook ) {
			?>
			<script type="text/javascript">
	var CourseTextbook<?php
			echo $courseTextbook->get_course_textbook_id ();
			?> = new CourseTextbook(<?php
			echo json_encode ( $courseTextbook );
			?>);
			</script>
	<tr>
		<td><?php
			echo $course->get_course_name ();
			?></td>
		<td><?php
			/* @var $department Department */
			$department_id = $course->get_department_id ();
			if (isset ( $department_id )) {
				$department = Department::constructFrom_department_id ( $department_id );
				echo $department->get_department_name ();
			}
			?></td>
		<td><?php
			/* @var $textbook Textbook */
			$textbook = Textbook::constructFrom_textbook_id ( $courseTextbook->get_textbook_id () );
			echo $textbook->get_textbook_name ();
			?></td>

		<td>
		<?php $courseTextbook->print_delete_button(true, "refreshPage");?>
		</td>
	</tr>
	
	<?php
		}
	}
}

?>
</table>

<br>
<br>
<h2>Add Textbook Requirement to Course</h2>
<script type="text/javascript">
var textbook_id;
function setTextbookId(id){
	textbook_id = id;
}

var course_id;
function setCourseId(id){
	course_id = id;
}
</script>
<?php Textbook::print_Textbook_dropdown(Textbook::getTextbooks(), "textbook_name", "", "setTextbookId"); ?>
<?php Course::print_Course_dropdown(Course::getCourses_by_instructor_id($instructor->get_instructor_id()), "course_name", "", "setCourseId");?>
<input class="ui-corner-all ui-state-default" type="button"
	onclick="insertNewCourseTextbook(course_id, textbook_id, refreshPage)"
	value="Add Textbook to Course"
/>
<br><br>
<h2>Textbooks</h2>
<?php Textbook::print_Textbook_add("refreshPage"); ?>
<table>
<tr>
<th>Textbook</th>
<th>ISBN</th>
</tr>
<?php 
	$textbooks = Textbook::getTextbooks();
	foreach($textbooks as $textbook){
?>
<tr>
<td><?php echo $textbook->get_textbook_name(); ?></td>
<td><?php echo $textbook->get_ISBN()?></td>
</tr>
<?php }?>

</table>
</body>
</html>
<?php
/**
 * Tells you if the department is in the list of the instructor departments
 *
 * @param int $department_id
 * @param array $instructorDepartments
 * @return boolean
 */
function instructorInDepartment($department_id, $instructorDepartments) {
	/* @var $instructorDepartment InstructorDepartment */
	foreach ( $instructorDepartments as $instructorDepartment ) {
		if ($department_id == $instructorDepartment->get_department_id ()) {
			return true;
		}
	}
	return false;
}

?>